<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-1-19
 * Time: 上午10:20
 */

namespace Admin\Controller;
use Mcit\Page;

class EmailController extends BaseController {
   public  function index(){

       //导入会员
       /*$coremember = D('Coremember')->field('username,nickname,sex,address,email,chexing')->where(1)->select();
       foreach($coremember as $v){
           $data['account'] = $v['username'];
           $data['phone'] = $v['username'];
           if($v['sex'] == 0){
               $data['sex'] = 2;
           }else{
               $data['sex'] = $v['sex'];
           }
           $data['nickname'] = $v['nickname'];
           $data['email'] = $v['email'];
           $data['vehicle'] = $v['chexing'];
           $data['address'] = $v['address'];
           M("member")->add($data);
       }*/

       //分页
       $count = M("email")->where("type=1")->count();
       vendor('Mcit.Sys.Page');
       $Page = new Page($count, 20);
       $pageView = $Page->show();
       $this->assign('pageView', $pageView); //发送分页
       $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
       $email = M("email")->where("type=1")->order("id desc")->page($nowPage . ',' . $Page->listRows)->select();
       //分页结束
       $this->email = $email;
       $this->display();
   }
    //发送邮件
    public function add(){

        $s_map['state'] = array('eq',1);
        $company = M('company')->field('name,id')->where($s_map)->select();
        $data['name'] = '系统管理员';
        $data['id']   = '0';
        $company[] = $data;
        $data1['name'] = '会员';
        $data1['id']   = '00';
        $company[] = $data1;
        $this->company = $company;

        $r_map['id'] = array('neq','805');
        $role = M('role')->field('name,id')->where($r_map)->select();
        $this->role = $role;

        $map['id'] = array('neq',1);
        $user = M('user')->field('company,email,name,id,department,role')->where($map)->select();
        $this->user = $user;

        $member = M('member')->field('email,id,nickname')->select();
        $this->member = $member;
        if(IS_POST){
            $mail = rtrim($_POST['mail'],',');
            $mails = explode(',',$mail);
            for($i=0;$i<count($mails);$i++)
            {
                $data['mail']      = $mails[$i];
                $data['title']     = $_POST['title'];//主题
                $data['content']   = $_POST['content'];//内容
                $data['send_time'] = date('Y-m-d H:i:s',time());//发送时间
                $result = M("email")->add($data);
                if($result > 0){
                    SendMail($mails[$i],$data['title'],$data['content']);
                }
            }
        }
        $this->display();
    }

    //查看邮件
    public function detail(){
        if(IS_GET){
            $map['id'] = I('id');
            $email = M('email')->where($map)->find();
            $email['content'] = strip_tags($email['content']);
            $this->assign('email',$email);
        }
        $this->display();
    }
    //删除信件
    public  function  delEmail(){

        if(IS_AJAX){
            $map['id'] = I('id');
            $result = M('email')->where($map)->delete();
            if($result){
                $return['state'] = 100;
                $return['msg']   = "删除邮件成功";
            }else{
                $return['state'] = 200;
                $return['msg']   = "删除失败";
            }
            echo json_encode($return);
            exit;
        }

    }
} 